﻿Public Class F22_No

    Dim DS As New List(Of TienNo)
    Public tra As Integer

    Sub HienThi()
        lsvno.Items.Clear()
        DS = XL_No.LayDS(Me.Tag)
        For Each n In DS
            Dim li As New ListViewItem(n.NgayBatDau)
            li.Tag = n
            li.SubItems.Add(n.SoTien)
            If n.Loai = False Then
                li.SubItems.Add("Lãi hàng tháng")
            Else
                li.SubItems.Add("Trả góp")
            End If
            li.SubItems.Add(n.ThoiHan)
            li.SubItems.Add(n.LaiSuat)
            li.SubItems.Add(n.DaTra)
            li.SubItems.Add(n.ChuNo)
            lsvno.Items.Add(li)
        Next
    End Sub

    Private Sub btnthanhtoan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthanhtoan.Click
        Try
            Dim n As TienNo = lsvno.SelectedItems(0).Tag
            Dim f = New F21_Them_Phieu_Chi
            f.Tag = Me.Tag
            f.ShowDialog()
            n.DaTra += tra
            XL_No.CapNhat(n)
        Catch ex As Exception

        End Try


    End Sub

    Private Sub btnthoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthoat.Click
        Me.Close()
    End Sub

    Private Sub F22_No_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cbx_loai.SelectedIndex = 0
        HienThi()
    End Sub

    Private Sub btnthem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthem.Click
        Try
            Dim n As New TienNo
            n.NgayBatDau = dtpngayno.Value.Date
            n.LanTraCuoi = n.NgayBatDau
            n.SoTien = Integer.Parse(txt_sotien.Text)
            n.NguoiDung = Me.Tag
            n.Loai = cbx_loai.SelectedIndex
            n.ThoiHan = Integer.Parse(txt_thoihan.Text)
            n.LaiSuat = Double.Parse(txt_laisuat.Text)
            n.DaTra = Integer.Parse(txt_datra.Text)
            n.ChuNo = txt_chuno.Text
            n.NoiDung = rtb_noidung.Text
            XL_No.Them(n)
            HienThi()
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End Try
    End Sub

    Private Sub btnxoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoa.Click
        Try
            Dim n As TienNo = lsvno.SelectedItems(0).Tag
            XL_No.Xoa(n)
            HienThi()
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub btnsua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsua.Click
        Try
            Dim n As TienNo = lsvno.SelectedItems(0).Tag
            n.NgayBatDau = dtpngayno.Value.Date
            n.LanTraCuoi = dtpngayno.Value.Date
            n.SoTien = Integer.Parse(txt_sotien.Text)
            n.NguoiDung = Me.Tag
            n.Loai = cbx_loai.SelectedIndex
            n.ThoiHan = Integer.Parse(txt_thoihan.Text)
            n.LaiSuat = Integer.Parse(txt_laisuat.Text)
            n.DaTra = Integer.Parse(txt_datra.Text)
            n.ChuNo = txt_chuno.Text
            n.NoiDung = rtb_noidung.Text
            XL_No.CapNhat(n)
            HienThi()
        Catch ex As Exception
            MessageBox.Show("Kiểm tra thông tin!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Exit Sub
        End Try
    End Sub

    Private Sub lsvno_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvno.SelectedIndexChanged
        Try
            Dim n As TienNo = lsvno.SelectedItems(0).Tag
            dtpngayno.Value = n.NgayBatDau
            txt_sotien.Text = n.SoTien.ToString()
            cbx_loai.SelectedIndex = n.Loai
            txt_thoihan.Text = n.ThoiHan.ToString()
            txt_laisuat.Text = n.LaiSuat.ToString()
            txt_datra.Text = n.DaTra.ToString()
            txt_chuno.Text = n.ChuNo
            rtb_noidung.Text = n.NoiDung
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub txt_sotien_TextChanged(sender As Object, e As EventArgs) Handles txt_sotien.TextChanged
        'txt_sotien.Text = String.Format("{0:0,0}", Decimal.Parse(txt_sotien.Text))
        'txt_sotien.SelectionStart = txt_sotien.Text.Length
    End Sub

    Private Sub txt_sotien_KeyUp(sender As Object, e As KeyEventArgs) Handles txt_sotien.KeyUp

    End Sub
End Class